Python for Teenagers by James R. Payne

Python for Teenagers by James R. Payne
Author: James R. Payne
Language: eng
Format: epub
ISBN: 9781484245507
Publisher: Apress

class Mutate(Superhero):

def __init__(self):

Superhero.__init__(self)

print("You created a Mutate!")

self.speed = self.speed + 10

# Creating a subclass of Superhero named Robot

# Robot heroes will get a + 10 bonus to their braun score.

class Robot(Superhero):

def __init__(self):

Superhero.__init__(self)

print("You created a robot!")

self.braun = self.braun + 10

Here, we have created two new classes, which are both actually subclasses of our Superhero class. The way we achieve this is by putting the name of the parent class in the parentheses of the newly created class. For example: class Mutate(Superhero) tells the Python interpreter to create a class that is a child or subclass of Superhero and inherit its methods and parameters.

We then initialize our new subclass using def __init__(self) and re-initialize our Superhero class using Superhero.__init__(self), since we will be creating new objects based off of, technically, both the class and subclass.

Finally, we want to give our heroes a bonus based off of which type of hero they are. The mutate character will receive a bonus to speed, as shown in this line of code:self.speed = self.speed + 10